import requests

# 登录成功
resp = requests.post(url="http://ihrm-test.itheima.net/api/sys/login",
                     headers={"Content-Type": "application/json"},
                     json={"mobile": "13800000002", "password": "123456"})
print("登录成功：", resp.json())

# 获取令牌
token = "Bearer " + resp.json().get("data")

# 添加员工
req_data = {"username": "小猪佩奇007", "mobile": "19278320017",
            "timeOfEntry": "2021-08-01", "formOfEmployment": 1,
            "workNumber": "009", "departmentName": "火星部",
            "departmentId": "1423981207154163712",
            "correctionTime": "2021-08-30T16:00:00.000Z"}

resp = requests.post(url="http://ihrm-test.itheima.net/api/sys/user",
                     headers={"Content-Type": "application/json",
                              "Authorization": token},
                     json=req_data)
print("添加员工成功：", resp.json())

# 获取员工id
emp_id = resp.json().get("data").get("id")
print("获取的员工id为：", emp_id)

# 查询员工
resp = requests.get(url="http://ihrm-test.itheima.net/api/sys/user/" + emp_id,
                    headers={"Content-Type": "application/json",
                             "Authorization": token}
                    )
print("查询员工结果：", resp.json())

# 修改员工
resp = requests.put(url="http://ihrm-test.itheima.net/api/sys/user/" + emp_id,
                    headers={"Content-Type": "application/json",
                             "Authorization": token},
                    json={"username": "大猪002"})
print("修改员工结果：", resp.json())

# 删除员工
resp = requests.delete(url="http://ihrm-test.itheima.net/api/sys/user/" + emp_id,
                       headers={"Content-Type": "application/json",
                                "Authorization": token}
                       )
print("删除员工结果：", resp.json())

# 查询员工列表
resp = requests.get(url="http://ihrm-test.itheima.net/api/sys/user?page=1&size=10",
                       headers={"Content-Type": "application/json",
                                "Authorization": token}
                       )
print("查询员工列表结果：", resp.json())
